package com.ol.edu.server.controller.course;


import com.ol.edu.server.entity.Course;
import com.ol.edu.server.entity.CourseLesson;
import com.ol.edu.server.entity.CourseSection;
import com.ol.edu.server.entity.ResponseResult;
import com.ol.edu.server.service.course.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author Roger
 * @date 2020/9/6 14:43
 * @description
 */
@RestController
@RequestMapping("/courseContent")
@CrossOrigin
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    /*
        根据课程ID查询章节与课时信息
     */
    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLesson(Integer courseId) {
        List<CourseSection> sectionList = courseContentService.findSectionAndLesson(courseId);
        return new ResponseResult(true, 200, "查询成功", sectionList);
    }

    /*
        回显章节对应的课程信息
     */
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(Integer courseId) {
        Course course = courseContentService.findCourseByCourseId(courseId);
        return new ResponseResult(true, 200, "查询成功", course);
    }

    /*
        保存和修改章节信息
     */
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection section) {
        //如果参数中ID为空，表示是添加操作
        if (section.getId() == null) {
            courseContentService.saveSection(section);
            return new ResponseResult(true, 200, "添加成功", null);
        } else {//更新操作
            courseContentService.updateSection(section);
            return new ResponseResult(true, 200, "修改成功", null);
        }
    }

    /*
        修改章节状态
     */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(Integer id, Integer status) {
        Map<String, Integer> map = courseContentService.updateSectionStatus(id, status);
        return new ResponseResult(true, 200, "修改章节状态成功", map);
    }

    /*
        保存课时信息
     */
    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveLesson(@RequestBody CourseLesson lesson) {
        if (lesson.getId() == null) {
            courseContentService.saveLesson(lesson);
            return new ResponseResult(true, 200, "保存课时信息成功", null);
        } else {
            courseContentService.updateLession(lesson);
            return new ResponseResult(true, 200, "修改课时信息成功", null);
        }
    }

}
